package com.yuan.zsschool.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yuan.base.framework.bean.TeachSchool;
import com.yuan.base.utils.DialogUtil;
import com.yuan.zsschool.AppContext;
import com.yuan.zsschool.R;

import java.util.List;

/**
 * 选择学校自定义控件
 */
public class SelectSchool extends LinearLayout {

	private LinearLayout ll_target_school;
	private TextView tv_target_school;

	private int currentSchoolId;
	private List<TeachSchool> teachSchoolList;

	private SelectConfirm selectConfirm;

	public SelectSchool(Context context) {
		super(context);
		init();
	}

	public SelectSchool(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public SelectSchool(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init();
	}

	private void init() {
		LayoutInflater.from(getContext()).inflate(R.layout.custom_select_school, this, true);
		ll_target_school = findViewById(R.id.ll_target_school);
		tv_target_school = findViewById(R.id.tv_target_school);
	}

	public void init(List<TeachSchool> teachSchoolList, SelectConfirm selectConfirm) {
		this.teachSchoolList = teachSchoolList;
		currentSchoolId = teachSchoolList.get(0).getSchoolId();
		tv_target_school.setText(teachSchoolList.get(0).getSchoolName());
		ll_target_school.setOnClickListener(v -> {
			DialogUtil.select(getContext(), "选择学校", AppContext.getSchoolNameArray(), (dialog, which) -> {
				TeachSchool teachSchool = teachSchoolList.get(which);
				currentSchoolId = teachSchool.getSchoolId();
				tv_target_school.setText(teachSchool.getSchoolName());
				if (selectConfirm != null) {
					selectConfirm.onSelect(teachSchool);
				}
			}, "取消", null);
		});
	}

	public int getCurrentSchoolId() {
		return currentSchoolId;
	}

	public String getCurrentSchoolName() {
		return tv_target_school.getText().toString();
	}

	public interface SelectConfirm {
		void onSelect(TeachSchool teachSchool);
	}
}
